// ITI 1120 Fall 2006, Lab 8, Exercise 1 // Name: Diana Inkpen, Student# 123456 /** * A recursive algorithm to test if all the characters in positions 0...N of an * array, S of characters are digits. */ class CheckDigits { public static void main (String[] args) { // DECLARE VARIABLES/DATA DICTIONARY char[] a; // an array of characters int n; // a position in the array. boolean result; // true if positions 0 to n // contain digit characters // PRINT OUT IDENTIFICATION INFORMATION System.out.println(); System.out.println("ITI1120 Fall 2006, Lab 8, Exercise 1"); System.out.println("Name: Diana Inkpen, Student# 123456"); System.out.println(); // READ IN GIVENS System.out.println("Please enter characters, all in one line (no spaces): "); a = ITI1120.readCharLine(); n = a.length; // BODY OF ALGORITHM result = checkDigits(a, n); // PRINT OUT RESULTS AND MODIFIEDS System.out.println("Only digits " + result); } // If the 'main' method calls other algorithms, put the method(s) below. /** * METHOD checkDigits * * This recursive method checks to see if all characters in a character array * are digit characters. * * GIVENS: arr -- a reference to an array of characters * n -- the "length" of the array (it is really only the * range of the array to check) */ public static boolean checkDigits( char[] arr, int n ) { // DECLARE VARIABLES/DATA DICTIONARY boolean allDigits; // RESULT: True if all characters in a are // digit characters. // BODY OF ALGORITHM if(n==0) { allDigits = true; } else { if( arr[n-1] >= '0' && arr[n-1] <= '9' ) { allDigits = checkDigits( arr, n-1 );; } else { allDigits = false; } } // RETURN RESULT return allDigits; } }